package com.zhonghui.mes.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.mes.domain.MesPlannedProduction;
import com.zhonghui.mes.domain.vo.MesMaterialApplyItemVo;
import com.zhonghui.mes.domain.vo.MesMaterialApplyVo;
import com.zhonghui.mes.domain.vo.MesPlannedProductionVo;
import com.zhonghui.mes.mapper.MesPlannedProductionMapper;
import com.zhonghui.mes.service.IMesPlannedProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.zhonghui.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhonghui.mes.mapper.MesMaterialApplyMapper;
import com.zhonghui.mes.domain.MesMaterialApply;
import com.zhonghui.mes.service.IMesMaterialApplyService;

/**
 * 领料申请Service业务层处理
 * 
 * @author zhonghui
 * @date 2022-05-21
 */
@Service
public class MesMaterialApplyServiceImpl implements IMesMaterialApplyService 
{
    @Autowired
    private MesMaterialApplyMapper mesMaterialApplyMapper;

    @Autowired
    private MesPlannedProductionMapper mesPlannedProductionMapper;

    @Autowired
    private IMesPlannedProductionService mesPlannedProductionService;

    /**
     * 查询领料申请
     * 
     * @param id 领料申请主键
     * @return 领料申请
     */
    @Override
    public MesMaterialApplyVo selectMesMaterialApplyById(Long id)
    {
        return mesMaterialApplyMapper.selectMesMaterialApplyById(id);
    }

    /**
     * 查询领料申请列表
     * 
     * @param mesMaterialApply 领料申请
     * @return 领料申请
     */
    @Override
    public List<MesMaterialApplyVo> selectMesMaterialApplyList(MesMaterialApply mesMaterialApply)
    {
        return mesMaterialApplyMapper.selectMesMaterialApplyList(mesMaterialApply);
    }

    /**
     * 新增领料申请
     * 
     * @param mesMaterialApplyVo 领料申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesMaterialApply(MesMaterialApplyVo mesMaterialApplyVo)
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        mesMaterialApplyVo.setOddNumber("LLSQ" + format.format(date));
        mesMaterialApplyVo.setApplicant(SecurityUtils.getUserId());
        mesMaterialApplyVo.setApplicantDate(new Date());
        int rows = mesMaterialApplyMapper.insertMesMaterialApply(mesMaterialApplyVo);
        insertMesMaterialApplyItem(mesMaterialApplyVo);
        // 更新计划排产领料状态
        MesPlannedProductionVo mesPlannedProductionVo = mesPlannedProductionMapper.selectMesPlannedProductionById(mesMaterialApplyVo.getPlannedProductionId());
        mesPlannedProductionVo.setPickingStatus(1);
        mesPlannedProductionMapper.updateMesPlannedProduction(mesPlannedProductionVo);
        return rows;
    }

    /**
     * 修改领料申请
     * 
     * @param mesMaterialApplyVo 领料申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesMaterialApply(MesMaterialApplyVo mesMaterialApplyVo)
    {
        mesMaterialApplyMapper.deleteMesMaterialApplyItemByApplyId(mesMaterialApplyVo.getId());
        insertMesMaterialApplyItem(mesMaterialApplyVo);
        return mesMaterialApplyMapper.updateMesMaterialApply(mesMaterialApplyVo);
    }

    /**
     * 批量删除领料申请
     * 
     * @param ids 需要删除的领料申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialApplyByIds(Long[] ids)
    {
        for (Long id : ids) {
            MesMaterialApplyVo mesMaterialApplyVo = mesMaterialApplyMapper.selectMesMaterialApplyById(id);
            MesPlannedProductionVo mesPlannedProductionVo = mesPlannedProductionMapper.selectMesPlannedProductionById(mesMaterialApplyVo.getPlannedProductionId());
            mesPlannedProductionVo.setPickingStatus(0);
            mesPlannedProductionMapper.updateMesPlannedProduction(mesPlannedProductionVo);
        }
        mesMaterialApplyMapper.deleteMesMaterialApplyItemByApplyIds(ids);
        return mesMaterialApplyMapper.deleteMesMaterialApplyByIds(ids);
    }

    /**
     * 删除领料申请信息
     * 
     * @param id 领料申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialApplyById(Long id)
    {
        MesMaterialApplyVo mesMaterialApplyVo = mesMaterialApplyMapper.selectMesMaterialApplyById(id);
        MesPlannedProductionVo mesPlannedProductionVo = mesPlannedProductionMapper.selectMesPlannedProductionById(mesMaterialApplyVo.getPlannedProductionId());
        mesPlannedProductionVo.setPickingStatus(0);
        mesPlannedProductionMapper.updateMesPlannedProduction(mesPlannedProductionVo);
        mesMaterialApplyMapper.deleteMesMaterialApplyItemByApplyId(id);
        return mesMaterialApplyMapper.deleteMesMaterialApplyById(id);
    }

    /**
     * 新增领料申请-生产计划明细子信息
     * 
     * @param mesMaterialApplyVo 领料申请对象
     */
    public void insertMesMaterialApplyItem(MesMaterialApplyVo mesMaterialApplyVo)
    {
        List<MesMaterialApplyItemVo> mesMaterialApplyItemList = mesMaterialApplyVo.getMesMaterialApplyItemList();
        Long id = mesMaterialApplyVo.getId();
        if (StringUtils.isNotNull(mesMaterialApplyItemList))
        {
            List<MesMaterialApplyItemVo> list = new ArrayList<MesMaterialApplyItemVo>();
            for (MesMaterialApplyItemVo mesMaterialApplyItem : mesMaterialApplyItemList)
            {
                mesMaterialApplyItem.setApplyId(id);
                list.add(mesMaterialApplyItem);
            }
            if (list.size() > 0)
            {
                mesMaterialApplyMapper.batchMesMaterialApplyItem(list);
            }
        }
    }

    /**
     * 设置领料单检验状态
     * @param mesMaterialApplyVo
     */
    @Transactional
    @Override
    public int checkMesMaterialApply(MesMaterialApplyVo mesMaterialApplyVo){
        MesMaterialApplyVo mesMaterialApply = mesMaterialApplyMapper.selectMesMaterialApplyById(mesMaterialApplyVo.getId());
        List<MesMaterialApplyItemVo> list = mesMaterialApply.getMesMaterialApplyItemList();
        int i = updateMesMaterialApply(mesMaterialApplyVo);
        // 检验合格
        if (mesMaterialApplyVo.getStatus() == 2) {
            // 更新排产领料状态
            MesPlannedProductionVo planned = mesPlannedProductionMapper.selectMesPlannedProductionById(mesMaterialApply.getPlannedProductionId());
            planned.setStatus(1);
            // 更新排产物料剩余数量
            planned.getMesPlannedProductionItemList().stream().forEach(detail -> {
                double left = detail.getResidueQuantity() == null ? 0 : detail.getResidueQuantity();
                // 查找物料领料数量
                List<MesMaterialApplyItemVo> mrq = list.stream()
                        .filter(rq -> rq.getMaterialId().longValue() == detail.getMaterialId().longValue())
                        .collect(Collectors.toList());
                if (mrq != null && !mrq.isEmpty()) {
                    MesMaterialApplyItemVo rqDetail = mrq.get(0);
                    double receive = rqDetail.getReceivedQuantity();
                    detail.setResidueQuantity(left + receive);
                }
            });
            mesPlannedProductionService.updateMesPlannedProduction(planned);
        }
        return i;
    }
}
